#region License
///License Begin
///This program has been developed by: Rodolfo Grave de Peralta, 
///Copyright (C) 2007 Rodolfo Grave de Peralta
///This program is free software; you can redistribute it and/or modify
///it under the terms of the GNU General Public License as published by
///the Free Software Foundation; either version 2 of the License, or
///(at your option) any later version.
///
///This program is distributed in the hope that it will be useful,
///but WITHOUT ANY WARRANTY; without even the implied warranty of
///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///GNU General Public License for more details.
///
///You should have received a copy of the GNU General Public License
///along with this program; if not, write to the Free Software
///Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
///License End
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace TEAM.TimeTracking.Model
{
    public abstract class Report : INotifyPropertyChanged
    {

        protected Predicate<object> filter;

        public virtual Predicate<object> Filter
        {
            get 
            {
                if (filter == null)
                {
                    filter = CreateFilter_Internal();
                }
                return filter; 
            }
        }

        protected abstract Predicate<object> CreateFilter_Internal();

        protected string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        protected ICollectionView taskExecutions;

        public ICollectionView TaskExecutions
        {
            get { return taskExecutions; }
            set 
            { 
                taskExecutions = value;
                OnPropertyChanged("TaskExecutions");
            }
        }

        protected TimeSpan? totalWorkingTime;

        public TimeSpan? TotalWorkingTime
        {
            get { return totalWorkingTime; }
        }

        protected TimeSpan? totalRestingTime;

        public TimeSpan? TotalRestingTime
        {
            get { return totalRestingTime; }
        }

        public void UpdateReport()
        {
            totalRestingTime = null;
            totalWorkingTime = null;
            if (TaskExecutions != null)
            {
                DateTime endTime;
                TimeSpan taskTime;
                totalRestingTime = new TimeSpan(0);
                totalWorkingTime = new TimeSpan(0);
                foreach (TaskExecution t in TaskExecutions)
                {
                    endTime = t.EndTime == null ? DateTime.Now : t.EndTime.Value;
                    taskTime = endTime.Subtract(t.StartTime);
                    if (t.Task.IsWorkingTime)
                    {
                        totalWorkingTime = totalWorkingTime.Value.Add(taskTime);
                    }
                    else
                    {
                        totalRestingTime = totalRestingTime.Value.Add(taskTime);
                    }
                }
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region Miembros de INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}
